const { defineConfig, build } = require('vite')
const vue = require('@vitejs/plugin-vue')
const path = require('path') // 主要用于alias文件路径别名

// https://vitejs.dev/config/
const baseConfig = defineConfig({
  configFile: false,
  publicDir: false,
  plugins: [vue()],
})

const rollupOptions = {
  // 确保外部化处理那些你不想打包进库的依赖
  external: ['vue', 'vue-router'], // 注意看这里
  output: {
    inlineDynamicImports: true, // umd 与 glob的问题
    // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
    globals: {
      vue: 'Vue',
      'vue-router': 'vueRouter',
    },
  },
}
const entryDir = path.resolve(__dirname, '../src/components')
const outDir = path.resolve(__dirname, '../lib')
const buildAll = async () => {
  await build({
    ...baseConfig,
    // 打包配置
    build: {
      rollupOptions,
      lib: {
        entry: entryDir,
        name: 'index',
        fileName: 'index',
        formats: ['es', 'umd'],
      },
      sourcemap: false,
      outDir,
    },
  })
}
const buildLib = async () => {
  await buildAll()
}

buildLib()
